<?php
class Model_Cup extends Zend_Db_Table_Abstract
{
	protected $_name    = 'g1_cup';
	protected $_primary = 'id';


	public function getAllActiveCups($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()
						->where('status=?', 'new')
						->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getAllCups($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()		
		->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getCupById($cup_id){
		$select = $this->select()
		               ->where('id=?',$cup_id);
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function createNewCup($cup_name,$group_count){
		
		$data['name'] = $cup_name;
		
		$data['group_count'] = $group_count;
		$data['status'] = 'new';
			
		$this->insert($data);
		$cup_id = $this->getAdapter()->lastInsertId();
		
		/*
		$groups = array('A','B','C','D',
				        'E','F','G','H',
				        'I','J','K','L',
				        'M','N','O','P');
		
		
		$model_cup_team = new Model_CupTeam();
		$count = 1;
		for($count=1;$count<$team_count +1;$count++){
			$model_cup_team->addTeam($cup_id,
					$count,
					$groups[($count-1)/ceil($team_count/$group_count)],
					'',
					null);
		}*/
		
		return $cup_id;
		
	}
	
	public function closeCupById($cup_id){
		$data = array('status'  => 'closed');
		
		$this->update($data, 'id='.$cup_id);
	}
	

	public function getFirstCupId(){
		$select = $this->select('id');
	
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function setTeamCount($cup_id,$team_count){
		$data = array('team_count'  => $team_count);
		
		$this->update($data, 'id='.$cup_id);
		
	}
}